package logic

import (
	"context"
	"database/sql"
	"goods/internal/model"
	"log"
	"strconv"
	"time"

	"goods/goods"
	"goods/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodsLogic {
	return &CreateGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加商品操作
func (l *CreateGoodsLogic) CreateGoods(in *goods.CreateGoodsRequest) (*goods.CreateGoodsResponse, error) {
	// todo: add your logic here and delete this line
	price, err := strconv.ParseFloat(in.Data.Price, 64)
	if err != nil {
		log.Println("类型转换错误")
	}
	goodsMsg := model.Goods{
		Name:        in.Data.Name,
		CategoryId:  in.Data.CategoryId,
		Price:       sql.NullFloat64{Float64: price, Valid: true},
		Image:       sql.NullString{String: in.Data.Image, Valid: true},
		Description: sql.NullString{String: in.Data.Description, Valid: true},
		Status:      int64(in.Data.Status),
		CreateTime:  sql.NullTime{Time: time.Now(), Valid: true},
	}
	_, err = l.svcCtx.Goods.Insert(context.Background(), &goodsMsg)
	if err != nil {
		log.Println("Error inserting category")
	}
	return &goods.CreateGoodsResponse{Code: 200, Msg: "添加商品成功"}, nil
}
